package net.sea.setup.ui.wizard.impl;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

import net.sea.commons.constants.AppConstants;
import net.sea.commons.util.InstallMsgUtils;
import net.sea.setup.action.Setup;
import net.sea.setup.config.custom.CustomConfig;
import net.sea.setup.ui.wizard.AbstractWizardPage;

/**
 * 安装中的页面
 * 
 * @author 华成伍
 *
 */
@SuppressWarnings("serial")
public class InstallingPage extends AbstractWizardPage {
	private JProgressBar installProgressBar = new JProgressBar();
	private JTextPane detailTextPane = new JTextPane();
	private Timer timer = new Timer();
	private boolean isCilcked = false;

	public InstallingPage() {
		contextInst.setAttribute(AppConstants.FINISH_FLAG_KEY, false);
		setBorder("\u7A0B\u5E8F\u5B89\u88C5\u4E2D\u2026\u2026");
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 10, 440, 10 };
		gridBagLayout.rowHeights = new int[] { 10, 10, 30, 10, 10, 300, 10 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0 };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0 };
		setLayout(gridBagLayout);

		JLabel tipLb = new JLabel(
				"\u6613\u5F00\u5E73\u53F0\u6B63\u5728\u5B89\u88C5\u4E2D\uFF0C\u8FD9\u9700\u8981\u4E00\u6BB5\u65F6\u95F4\uFF0C\u8BF7\u60A8\u7A0D\u7B49\u3002");
		GridBagConstraints gbc_tipLb = new GridBagConstraints();
		gbc_tipLb.anchor = GridBagConstraints.WEST;
		gbc_tipLb.insets = new Insets(0, 0, 5, 5);
		gbc_tipLb.gridx = 1;
		gbc_tipLb.gridy = 1;
		add(tipLb, gbc_tipLb);

		installProgressBar.setStringPainted(true);
		GridBagConstraints gbc_installProgressBar = new GridBagConstraints();
		gbc_installProgressBar.insets = new Insets(0, 0, 5, 5);
		gbc_installProgressBar.fill = GridBagConstraints.HORIZONTAL;
		gbc_installProgressBar.gridx = 1;
		gbc_installProgressBar.gridy = 3;
		add(installProgressBar, gbc_installProgressBar);

		JScrollPane detailScrollPane = new JScrollPane();
		GridBagConstraints gbc_detailScrollPane = new GridBagConstraints();
		gbc_detailScrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_detailScrollPane.fill = GridBagConstraints.BOTH;
		gbc_detailScrollPane.gridx = 1;
		gbc_detailScrollPane.gridy = 5;
		add(detailScrollPane, gbc_detailScrollPane);

		detailScrollPane.setViewportView(detailTextPane);
	}

	@Override
	public PageResult validateInput() {
		return new PageResult(true);
	}

	/**
	 * 安装进度任务
	 * 
	 * @author 华成伍
	 *
	 */
	class InstallTask extends TimerTask {

		public void run() {
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					if ((Boolean) contextInst
							.getAttribute(AppConstants.FINISH_FLAG_KEY)) {
						// if(true){
						timer.cancel();
						if (!isCilcked) {
							// 获取下一步按钮
							List<JButton> buttons = getParent().getButtons();
							JButton nextBtn = null;
							for (JButton button : buttons) {
								if ("下一步".equals(button.getText().trim())) {
									nextBtn = button;
									break;
								}
							}
							// 手工调用下一步按钮的点击事件
							nextBtn.doClick();
							isCilcked = true;
						}
					} else {
						installProgressBar.setValue(InstallMsgUtils
								.getInstallProcess());
						if (installProgressBar.getValue() < 100) {
							installProgressBar.repaint();
						}
						detailTextPane.setText(InstallMsgUtils.getInstallMsg());
					}
				}
			});
		}
	}

	/**
	 * 页面准备
	 */
	@Override
	public void ready() {
		super.ready();
	}

	@Override
	public void process() {
		super.process();
		Thread setupThread = new Thread(new Runnable() {

			@Override
			public void run() {
				Setup setup = new Setup(contextInst);
				setup.start(Setup.INSTALL);
			}
		});
		setupThread.setName("installThread");

		timer.schedule(new InstallTask(), 10, 1000);
		contextInst.setAttribute(AppConstants.NEEDEDS_BUTTONS_KEY,
				new boolean[] { false, false, false, true });
		contextInst.setAttribute(AppConstants.ROLLBACK_FLAG, true);
		contextInst.setAttribute(AppConstants.SETUP_THREAD_KEY, setupThread);
		setupThread.start();
	}

	@Override
	protected List<CustomConfig> createCustomConfig() {
		return null;
	}
}
